package net.steam.easyjoboa.web.action;

import java.util.List;


import net.steam.easyjoboa.biz.ClaimVoucherBiz;
import net.steam.easyjoboa.biz.EmployeeBiz;
import net.steam.easyjoboa.entity.ClaimVoucher;
import net.steam.easyjoboa.entity.Employee;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;


public class ClaimVoucherAction {
	private ClaimVoucher claim;
	private List<ClaimVoucher> claimList;
	private ClaimVoucherBiz claimBiz;
	private Employee employee;
	private EmployeeBiz employeeBiz;
	private String[] updateCbo; //获取页面checkbox name
	private String[] claimSubmit; //获取页面submit name

	/**
	 * 添加报销申请
	 * @return
	 */
	@Action(value = "addClaim", results = { @Result(name = "success",type="redirectAction",location = "getClaimByUser"),
			@Result(name = "error", location = "/error.jsp") })
	public String addClaim() {
		try {
			employee = employeeBiz.findEmployeeById(employee.getUserId());
			claim.setStatus("0");
			claim.setEmployee(employee);
			claimBiz.addClaim(claim);
				return "success";
		}catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}

	/**
	 * 管理员-查找(不包括自己)未审批的报销单
	 * @return
	 */
	@Action(value = "getAllClaim", results = {
			@Result(name = "success", location = "/Claim_list.jsp"),
			@Result(name = "error", location = "/error.jsp") })
	public String getAllClaim() {
		employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
		claimList = claimBiz.getAllClaimVouchers(employee.getUserId());
		if (null != claimList) {
			return "success";
		} else {
			return "error";
		}
	}
	
	/**
	 * 查找员工个人报销信息
	 * @return
	 */
	@Action(value = "getClaimByUser", results = {
			@Result(name = "success", location = "/Claim_me.jsp"),
			@Result(name = "error", location = "/error.jsp") })
	public String getClaimByUser() {
		employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		claimList=claimBiz.getClaimByUser(employee.getUserId());
		if (null != claimList) {
			return "success";
		} else {
			return "error";
		}
	}

	/**
	 * 根据id查找->申请报销
	 * @return
	 */
	@Action(value = "toClaim", results = {
			@Result(name = "success", location = "/Claim_add.jsp")})
	public String claimById() {
		employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		return "success";
	}
	
	/**
	 * 管理员-批量更新报销单->批准.拒绝
	 * @return
	 */
	@Action(value = "updateClaim", results = {
			@Result(name = "success",type="redirect",location = "getAllClaim"),
			@Result(name = "error", location = "/error.jsp")})
	public String updateClaim() {
		if(claimSubmit[0].equals("批 准")) {
			int[] cvId=new int[updateCbo.length];
			for(int i=0;i<updateCbo.length;i++){
				cvId[i]=Integer.parseInt(updateCbo[i]);
				int id=cvId[i];
				claim = claimBiz.getClaimById(id); //根据id得到实体类
				claim.setStatus("1"); //批准状态为1
				claimBiz.updateClaim(claim);
			}
		}else if(claimSubmit[0].equals("拒 绝")) {
			int[] cvId=new int[updateCbo.length];
			for(int i=0;i<updateCbo.length;i++){
				cvId[i]=Integer.parseInt(updateCbo[i]);
				int id=cvId[i];
				claim = claimBiz.getClaimById(id); //根据id得到实体类
				claim.setStatus("2"); //拒绝状态为2
				claimBiz.updateClaim(claim);
			}
		}
			return "success";
		}
	

	
	// get set
	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public ClaimVoucher getClaim() {
		return claim;
	}

	public void setClaim(ClaimVoucher claim) {
		this.claim = claim;
	}

	public ClaimVoucherBiz getClaimBiz() {
		return claimBiz;
	}

	public void setClaimBiz(ClaimVoucherBiz claimBiz) {
		this.claimBiz = claimBiz;
	}

	public List<ClaimVoucher> getClaimList() {
		return claimList;
	}

	public void setClaimList(List<ClaimVoucher> claimList) {
		this.claimList = claimList;
	}

	public String[] getClaimSubmit() {
		return claimSubmit;
	}

	public void setClaimSubmit(String[] claimSubmit) {
		this.claimSubmit = claimSubmit;
	}

	public String[] getUpdateCbo() {
		return updateCbo;
	}

	public void setUpdateCbo(String[] updateCbo) {
		this.updateCbo = updateCbo;
	}

	public EmployeeBiz getEmployeeBiz() {
		return employeeBiz;
	}

	public void setEmployeeBiz(EmployeeBiz employeeBiz) {
		this.employeeBiz = employeeBiz;
	}

}
